﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hartley.Entities;
using Hartley.DTOs;
namespace Hartley.BusinessLogic
{
    public class MemberManager
    {
        HCPPADataContext hcDc;
        const int maxNoOfPresident = 1;
        const int maxNoOfPatron =1;
        const int maxNoOfVicePatron = 5;
        const int maxNoOfVicePresident = 10;
        const int maxNoOfSecretary = 1;
        const int maxNoOfAsstSecreatary = 1;
        const int maxNoOfTreasurer = 1;
        const int maxNoOfAsstTreasurer = 1;
        const int maxNoOfAuditor = 1;
        const int maxNoOfEditor = 1;
        const int maxNoOfCommMember = 15;
        const int maxNoOfImmPastPresident = 1;
        public MemberManager(HCPPADataContext hcDc)
        {
            this.hcDc = hcDc;
        }
        public string SaveMember(MemberDTO memberDTO)
        {
            string result = "";
            try{
            var membr = hcDc.hcMembers.Where(n => n.MemberId == memberDTO.MemberId).SingleOrDefault();
            if (membr == null)
            {
                

                    hcMember member = new hcMember
                    {
                        MemberId = memberDTO.MemberId,
                        FirstName = memberDTO.FirstName,
                        LastName = memberDTO.LastName,
                        Title = memberDTO.Title,
                        DateOfBirth = Convert.ToDateTime(memberDTO.DateOfBirth),
                        TelePhoneNo = memberDTO.TelePhoneNo,
                       // MobileNo = memberDTO.MobileNo,
                        Email = memberDTO.Email,
                        PermanantAddress = memberDTO.PermanantAddress,
                        TemporaryAddress = memberDTO.TemporaryAddress,
                        OrdinaryBatchYear = memberDTO.OrdinaryBatchYear,
                        Division = memberDTO.Division,
                        SchoolIndexNo = memberDTO.SchoolIndexNo,
                        LifeMemberNo = memberDTO.LifeMemberNo,
                        Designation = memberDTO.Designation,
                        WorkingAt = memberDTO.WorkingAt,
                        StudiedFrom = memberDTO.StudiedFrom,
                        StudiedUpto = memberDTO.StudiedUpto,
                        House = memberDTO.House,
                        MemberType = memberDTO.MemberType,
                        UserName = memberDTO.UserName,
                        Password = memberDTO.Password,
                        SecurityQuestion = memberDTO.SecurityQuestion,
                        SecurityAnswer = memberDTO.SecurityAnswer,
                        Descripion = memberDTO.Description,
                        Status = memberDTO.Status,
                        PhotoPath = memberDTO.PhotoPath,
                        ModifiedBy = memberDTO.ModifiedBy,
                        ModifiedTime = memberDTO.ModifiedTime
                    };
                    hcDc.hcMembers.InsertOnSubmit(member);
                    result = "Saved";
                

            }
            else
            {
                //Edit
                result = "Updated";
            }

            hcDc.SubmitChanges();
            }
            catch
            {
                throw;
            }
            return result;
        }

        public string SaveCommittee(CommitteeDTO committeeDTO)
        {
            string result="";
            try{

            var comte = hcDc.hcCommittees.Where(n => n.CommitteeId == committeeDTO.CommitteeId).SingleOrDefault();
            if (comte == null)
            {
                //If the same committee name is not written then allow him to create the comittee
                var comteName = hcDc.hcCommittees.Where(a => a.Title == committeeDTO.Title).SingleOrDefault();
                if (comteName == null)
                {
                   

                        hcCommittee committee = new hcCommittee
                        {
                            CommitteeId = committeeDTO.CommitteeId,
                            Title = committeeDTO.Title,
                            StartDate = committeeDTO.StartDate,
                            EndDate = committeeDTO.EndDate,
                            CommitteePhotoName = committeeDTO.CommitteePhotoName,
                            CommitteePhotoPath = committeeDTO.CommitteePhotoPath,
                            Description = committeeDTO.Description,
                            ModifiedBy = committeeDTO.ModifiedBy,
                            ModifiedTime = committeeDTO.ModifiedTime
                        };
                        hcDc.hcCommittees.InsertOnSubmit(committee);
                        result = "Saved";
                   
                }

                else
                {
                    //If the same committee name is written then give an error message to the user
                    result = "AlreadyExists";
                }

            }

            else
            {
                //Edit

                comte.Title =committeeDTO.Title;
                comte.StartDate =committeeDTO.StartDate;
                comte.EndDate =committeeDTO.EndDate;
                comte.CommitteePhotoName =committeeDTO.CommitteePhotoName;
                comte.CommitteePhotoPath =committeeDTO.CommitteePhotoPath;
                comte.Description =committeeDTO.Description;
                comte.ModifiedBy =committeeDTO.ModifiedBy;
                comte.ModifiedTime =committeeDTO.ModifiedTime;
                
                result = "Updated";
            }

            hcDc.SubmitChanges();
            }
            catch
            {
                throw;
            }
            return result;
        }

        //public string SaveCommiteeMembers(CommitteeMemberDTO committeeMemberDTO)
        //{
        //    string result="";
        //   try
        //    {
        //    var commMembr = hcDc.hcCommitteeMembers.Where(n => n.CommitteeId == committeeMemberDTO.CommitteId && n.MemberId == committeeMemberDTO.MemberId).SingleOrDefault();
        //    if (commMembr == null)
        //    {
        //        //Below postions can be designated by only one man,not more.
        //        //string[] oneManPostions = new string[] { "President", "Immediate Past president", "Patron", "Secretary", "Asst.Secretary", "Asst.Treasurer", "Treasurer" };
        //        //List<hcCommitteeMember> commMembrs = hcDc.hcCommitteeMembers.Where(a=> a.CommitteeId == committeeMemberDTO.CommitteId).ToList();

        //        var comteFrm = hcDc.hcCommittees.Where(a => a.CommitteeId == committeeMemberDTO.CommitteId).SingleOrDefault().StartDate;
        //        var comteTo = hcDc.hcCommittees.Where(a => a.CommitteeId == committeeMemberDTO.CommitteId).SingleOrDefault().EndDate;
        //        bool insertCommMember = false;
              
        //            if (committeeMemberDTO.Position == "President")
        //            {
        //                var varPresident = hcDc.hcCommitteeMembers.Where(c => c.CommitteeId == committeeMemberDTO.CommitteId && c.Position == "President").SingleOrDefault();
        //                if (varPresident == null)
        //                {
        //                    insertCommMember = true;
        //                }
        //                else
        //                {
        //                    result = "onlyOneMemberOfthisRoleCanExists";
        //                }
        //            }

        //            else if (committeeMemberDTO.Position == "Immediate Past president")
        //            {
        //                var varImmPresdident = hcDc.hcCommitteeMembers.Where(c => c.CommitteeId == committeeMemberDTO.CommitteId && c.Position == "Immediate Past president").SingleOrDefault();

        //                if (varImmPresdident == null)
        //                {
        //                    insertCommMember = true;
        //                }
        //                else
        //                {
        //                    result = "onlyOneMemberOfthisRoleCanExists";
        //                }
        //            }

        //            else if (committeeMemberDTO.Position == "Patron")
        //            {
        //                var varPatron = hcDc.hcCommitteeMembers.Where(c => c.CommitteeId == committeeMemberDTO.CommitteId && c.Position == "Patron").SingleOrDefault();

        //                if (varPatron == null)
        //                {
        //                    insertCommMember = true;
        //                }
        //                else
        //                {
        //                    result = "onlyOneMemberOfthisRoleCanExists";
        //                }
        //            }
        //            else if (committeeMemberDTO.Position == "Secretary")
        //            {
        //                var varSecy = hcDc.hcCommitteeMembers.Where(c => c.CommitteeId == committeeMemberDTO.CommitteId && c.Position == "Secretary").SingleOrDefault();

        //                if (varSecy == null)
        //                {
        //                    insertCommMember = true;
        //                }
        //                else
        //                {
        //                    result = "onlyOneMemberOfthisRoleCanExists";
        //                }
        //            }
        //            else if (committeeMemberDTO.Position == "Asst.Secretary")
        //            {
        //                var varAsstSecy = hcDc.hcCommitteeMembers.Where(c => c.CommitteeId == committeeMemberDTO.CommitteId && c.Position == "Asst.Secretary").SingleOrDefault();

        //                if (varAsstSecy == null)
        //                {
        //                    insertCommMember = true;
        //                }
        //                else
        //                {
        //                    result = "onlyOneMemberOfthisRoleCanExists";
        //                }
        //            }
        //            else if (committeeMemberDTO.Position == "Treasurer")
        //            {
        //                var varTreasurer = hcDc.hcCommitteeMembers.Where(c => c.CommitteeId == committeeMemberDTO.CommitteId && c.Position == "Treasurer").SingleOrDefault();

        //                if (varTreasurer == null)
        //                {
        //                    insertCommMember = true;
        //                }
        //                else
        //                {
        //                    result = "onlyOneMemberOfthisRoleCanExists";
        //                }
        //            }
        //            else if (committeeMemberDTO.Position == "Asst.Treasurer")
        //            {
        //                var varAsstTreasurer = hcDc.hcCommitteeMembers.Where(c => c.CommitteeId == committeeMemberDTO.CommitteId && c.Position == "Asst.Treasurer").SingleOrDefault();

        //                if (varAsstTreasurer == null)
        //                {
        //                    insertCommMember = true;
        //                }
        //                else
        //                {
        //                    result = "onlyOneMemberOfthisRoleCanExists";
        //                }
        //            }

        //            else if (committeeMemberDTO.Position == "Vice President" || committeeMemberDTO.Position == "Vice Patron" || committeeMemberDTO.Position == "Committee Member")
        //            {

        //                insertCommMember = true;

        //            }

        //            if (insertCommMember)
        //            {
        //                if (committeeMemberDTO.MemberFrom >= comteFrm && committeeMemberDTO.MemberTo <= comteTo)
        //                {
        //                    //Then only his period is valid
        //                    hcCommitteeMember committeeMember = new hcCommitteeMember
        //                    {
        //                        CommitteeId = committeeMemberDTO.CommitteId,
        //                        MemberId = committeeMemberDTO.MemberId,
        //                        MemberFrom = committeeMemberDTO.MemberFrom,
        //                        MemberTo = committeeMemberDTO.MemberTo,
        //                        Position = committeeMemberDTO.Position,
        //                        Description = committeeMemberDTO.Description,
        //                        ModifiedBy = committeeMemberDTO.ModifiedBy,
        //                        ModifiedTime = Convert.ToDateTime( committeeMemberDTO.ModifiedTime)
        //                    };
        //                    hcDc.hcCommitteeMembers.InsertOnSubmit(committeeMember);
        //                    result = "Saved";
        //                }
        //                else
        //                {
        //                    // Give him a error message saying memeber's period is not valid
        //                    result = "MemberPeriodInValid";
        //                }



        //            }






               




        //    }
        //    else
        //    {
        //        //Update
        //        result = "Updated";
        //    }

        //    hcDc.SubmitChanges();
        //    }

        //   catch
        //   {
        //       throw;
        //   }

        //    return result;
        //}


        public string SaveCommiteeMembers(CommitteeMemberDTO committeeMemberDTO)
        {
            string result = "";
            bool insertCommMember = false;
            try
            {
                var comiteMembr = hcDc.hcCommitteeMembers.Where(n => n.CommitteeId == committeeMemberDTO.CommitteId && n.MemberId == committeeMemberDTO.MemberId).SingleOrDefault();
                if (comiteMembr == null)
                {

                    if (committeeMemberDTO.Position == "Patron")
                    {
                        var varPatron = hcDc.hcCommitteeMembers.Where(c => c.CommitteeId == committeeMemberDTO.CommitteId && c.Position == "Patron").ToList().Count;
                        if (varPatron < maxNoOfPatron)
                        {
                            insertCommMember = true;
                        }
                        else
                        {
                            // result = "NoOfMembersForThisRoleExceeded";
                            insertCommMember = false;
                        }
                    }

                    if (committeeMemberDTO.Position == "Vice Patron")
                    {
                        var varPatron = hcDc.hcCommitteeMembers.Where(c => c.CommitteeId == committeeMemberDTO.CommitteId && c.Position == "Vice Patron").ToList().Count;
                        if (varPatron < maxNoOfVicePatron)
                        {
                            insertCommMember = true;
                        }
                        else
                        {
                            // result = "NoOfMembersForThisRoleExceeded";
                            insertCommMember = false;
                        }
                    }

                    if (committeeMemberDTO.Position == "Immediate Past president")
                    {
                        var varPatron = hcDc.hcCommitteeMembers.Where(c => c.CommitteeId == committeeMemberDTO.CommitteId && c.Position == "Immediate Past president").ToList().Count;
                        if (varPatron < maxNoOfImmPastPresident)
                        {
                            insertCommMember = true;
                        }
                        else
                        {
                            // result = "NoOfMembersForThisRoleExceeded";
                            insertCommMember = false;
                        }
                    }

                    else if (committeeMemberDTO.Position == "President")
                    {
                        var varPresident = hcDc.hcCommitteeMembers.Where(c => c.CommitteeId == committeeMemberDTO.CommitteId && c.Position == "President").ToList().Count;

                        if (varPresident < maxNoOfPresident)
                        {
                            insertCommMember = true;
                        }
                        else
                        {
                            //result = "NoOfMembersForThisRoleExceeded";
                            insertCommMember = false;
                        }
                    }

                    else if (committeeMemberDTO.Position == "Secretary")
                    {
                        var varSecy = hcDc.hcCommitteeMembers.Where(c => c.CommitteeId == committeeMemberDTO.CommitteId && c.Position == "Secretary").ToList().Count;

                        if (varSecy < maxNoOfSecretary)
                        {
                            insertCommMember = true;
                        }
                        else
                        {
                            //result = "NoOfMembersForThisRoleExceeded";
                            insertCommMember = false;
                        }
                    }
                    else if (committeeMemberDTO.Position == "Asst.Secretary")
                    {
                        var varAsstSecy = hcDc.hcCommitteeMembers.Where(c => c.CommitteeId == committeeMemberDTO.CommitteId && c.Position == "Asst.Secretary").ToList().Count;

                        if (varAsstSecy < maxNoOfAsstSecreatary)
                        {
                            insertCommMember = true;
                        }
                        else
                        {
                            //result = "NoOfMembersForThisRoleExceeded";
                            insertCommMember = false;
                        }
                    }

                    else if (committeeMemberDTO.Position == "Treasurer")
                    {
                        var varTreasurer = hcDc.hcCommitteeMembers.Where(c => c.CommitteeId == committeeMemberDTO.CommitteId && c.Position == "Treasurer").ToList().Count;

                        if (varTreasurer < maxNoOfTreasurer)
                        {
                            insertCommMember = true;
                        }
                        else
                        {
                            // result = "NoOfMembersForThisRoleExceeded";
                            insertCommMember = false;
                        }
                    }
                    else if (committeeMemberDTO.Position == "Asst.Treasurer")
                    {
                        var varAsstTreasurer = hcDc.hcCommitteeMembers.Where(c => c.CommitteeId == committeeMemberDTO.CommitteId && c.Position == "Asst.Treasurer").ToList().Count;

                        if (varAsstTreasurer < maxNoOfAsstTreasurer)
                        {
                            insertCommMember = true;
                        }
                        else
                        {
                            //result = "NoOfMembersForThisRoleExceeded";
                            insertCommMember = false;
                        }
                    }

                    else if (committeeMemberDTO.Position == "Vice President")
                    {
                        var varVisePresident = hcDc.hcCommitteeMembers.Where(c => c.CommitteeId == committeeMemberDTO.CommitteId && c.Position == "Vice President").ToList().Count;

                        if (varVisePresident < maxNoOfVicePresident )
                        {
                            insertCommMember = true;
                        }
                        else
                        {
                            //result = "NoOfMembersForThisRoleExceeded";
                            insertCommMember = false;
                        }
                    }

                    else if (committeeMemberDTO.Position == "Auditor")
                    {
                        var varAuditor = hcDc.hcCommitteeMembers.Where(c => c.CommitteeId == committeeMemberDTO.CommitteId && c.Position == "Auditor").ToList().Count;

                        if (varAuditor < maxNoOfAuditor)
                        {
                            insertCommMember = true;
                        }
                        else
                        {
                            // result = "NoOfMembersForThisRoleExceeded";
                            insertCommMember = false;
                        }
                    }

                    else if (committeeMemberDTO.Position == "Editor")
                    {
                        var varEditor = hcDc.hcCommitteeMembers.Where(c => c.CommitteeId == committeeMemberDTO.CommitteId && c.Position == "Editor").ToList().Count;

                        if (varEditor < maxNoOfEditor)
                        {
                            insertCommMember = true;
                        }
                        else
                        {
                            // result = "NoOfMembersForThisRoleExceeded";
                            insertCommMember = false;
                        }
                    }

                    else if (committeeMemberDTO.Position == "Committee Member")
                    {
                        var varCommitteeMember = hcDc.hcCommitteeMembers.Where(c => c.CommitteeId == committeeMemberDTO.CommitteId && c.Position == "Committee Member").ToList().Count;

                        if (varCommitteeMember < maxNoOfCommMember)
                        {
                            insertCommMember = true;
                        }
                        else
                        {
                            //  result = "NoOfMembersForThisRoleExceeded";
                            insertCommMember = false;
                        }
                    }

                    if (insertCommMember)
                    {
                        string comteStart = hcDc.hcCommittees.Where(a => a.CommitteeId == committeeMemberDTO.CommitteId).FirstOrDefault().StartDate.ToString();
                        string comteEnd = hcDc.hcCommittees.Where(a => a.CommitteeId == committeeMemberDTO.CommitteId).FirstOrDefault().EndDate.ToString();

                        //Check some time it may throw exceptions in case if the comm start ,end date note exits
                        if (comteStart != null && comteEnd != null)
                        {
                            //If it's nt null then compare with the ccommiitee start & end data to make sure member''s period exceed committee period
                            DateTime comteStartDate = Convert.ToDateTime(comteStart);
                            DateTime comteEndDate = Convert.ToDateTime(comteEnd);

                            if (committeeMemberDTO.MemberFrom != null && committeeMemberDTO.MemberTo != null)
                            {
                                if (committeeMemberDTO.MemberFrom < comteStartDate || committeeMemberDTO.MemberTo > comteEndDate)
                                {
                                    result = "InvalidMemberPeriod";
                                }
                                else
                                {
                                    hcCommitteeMember committeeMember = new hcCommitteeMember
                                    {
                                        CommitteeId = committeeMemberDTO.CommitteId,
                                        MemberId = committeeMemberDTO.MemberId,
                                        MemberFrom = committeeMemberDTO.MemberFrom,
                                        MemberTo = committeeMemberDTO.MemberTo,
                                        Position = committeeMemberDTO.Position,
                                        Description = committeeMemberDTO.Description,
                                        ModifiedBy = committeeMemberDTO.ModifiedBy,
                                        ModifiedTime = committeeMemberDTO.ModifiedTime
                                    };
                                    hcDc.hcCommitteeMembers.InsertOnSubmit(committeeMember);
                                    result = "Saved";
                                }


                            }
                            else
                            {
                                //this wont become nnull as we are passing date from Application logic
                            }



                        }
                        else
                        {
                            result = "InValidCommittee";
                        }
                    }



                    else
                    {
                        result = "NoOfMembersForThisRoleExceeded";

                    }

                }
                else
                {
                    //Edit

                    if (committeeMemberDTO.Position == "Patron")
                    {
                        var varPatron = hcDc.hcCommitteeMembers.Where(c => c.CommitteeId == committeeMemberDTO.CommitteId && c.Position == "Patron").ToList().Count;
                        if (varPatron < maxNoOfPatron)
                        {
                            insertCommMember = true;
                        }
                        else
                        {
                            // result = "NoOfMembersForThisRoleExceeded";
                            insertCommMember = false;
                        }
                    }

                    if (committeeMemberDTO.Position == "Vice Patron")
                    {
                        var varPatron = hcDc.hcCommitteeMembers.Where(c => c.CommitteeId == committeeMemberDTO.CommitteId && c.Position == "Vice Patron").ToList().Count;
                        if (varPatron < maxNoOfVicePatron)
                        {
                            insertCommMember = true;
                        }
                        else
                        {
                            // result = "NoOfMembersForThisRoleExceeded";
                            insertCommMember = false;
                        }
                    }

                    if (committeeMemberDTO.Position == "Immediate Past president")
                    {
                        var varPatron = hcDc.hcCommitteeMembers.Where(c => c.CommitteeId == committeeMemberDTO.CommitteId && c.Position == "Immediate Past president").ToList().Count;
                        if (varPatron < maxNoOfImmPastPresident)
                        {
                            insertCommMember = true;
                        }
                        else
                        {
                            // result = "NoOfMembersForThisRoleExceeded";
                            insertCommMember = false;
                        }
                    }

                    else if (committeeMemberDTO.Position == "President")
                    {
                        var varPresident = hcDc.hcCommitteeMembers.Where(c => c.CommitteeId == committeeMemberDTO.CommitteId && c.Position == "President").ToList().Count;

                        if (varPresident < maxNoOfPresident)
                        {
                            insertCommMember = true;
                        }
                        else
                        {
                            //result = "NoOfMembersForThisRoleExceeded";
                            insertCommMember = false;
                        }
                    }

                    else if (committeeMemberDTO.Position == "Secretary")
                    {
                        var varSecy = hcDc.hcCommitteeMembers.Where(c => c.CommitteeId == committeeMemberDTO.CommitteId && c.Position == "Secretary").ToList().Count;

                        if (varSecy < maxNoOfSecretary)
                        {
                            insertCommMember = true;
                        }
                        else
                        {
                            //result = "NoOfMembersForThisRoleExceeded";
                            insertCommMember = false;
                        }
                    }
                    else if (committeeMemberDTO.Position == "Asst.Secretary")
                    {
                        var varAsstSecy = hcDc.hcCommitteeMembers.Where(c => c.CommitteeId == committeeMemberDTO.CommitteId && c.Position == "Asst.Secretary").ToList().Count;

                        if (varAsstSecy < maxNoOfAsstSecreatary)
                        {
                            insertCommMember = true;
                        }
                        else
                        {
                            //result = "NoOfMembersForThisRoleExceeded";
                            insertCommMember = false;
                        }
                    }

                    else if (committeeMemberDTO.Position == "Treasurer")
                    {
                        var varTreasurer = hcDc.hcCommitteeMembers.Where(c => c.CommitteeId == committeeMemberDTO.CommitteId && c.Position == "Treasurer").ToList().Count;

                        if (varTreasurer < maxNoOfTreasurer)
                        {
                            insertCommMember = true;
                        }
                        else
                        {
                            // result = "NoOfMembersForThisRoleExceeded";
                            insertCommMember = false;
                        }
                    }
                    else if (committeeMemberDTO.Position == "Asst.Treasurer")
                    {
                        var varAsstTreasurer = hcDc.hcCommitteeMembers.Where(c => c.CommitteeId == committeeMemberDTO.CommitteId && c.Position == "Asst.Treasurer").ToList().Count;

                        if (varAsstTreasurer < maxNoOfAsstTreasurer)
                        {
                            insertCommMember = true;
                        }
                        else
                        {
                            //result = "NoOfMembersForThisRoleExceeded";
                            insertCommMember = false;
                        }
                    }

                    else if (committeeMemberDTO.Position == "Vice President")
                    {
                        var varVisePresident = hcDc.hcCommitteeMembers.Where(c => c.CommitteeId == committeeMemberDTO.CommitteId && c.Position == "Vice President").ToList().Count;

                        if (varVisePresident < maxNoOfVicePresident)
                        {
                            insertCommMember = true;
                        }
                        else
                        {
                            //result = "NoOfMembersForThisRoleExceeded";
                            insertCommMember = false;
                        }
                    }

                    else if (committeeMemberDTO.Position == "Auditor")
                    {
                        var varAuditor = hcDc.hcCommitteeMembers.Where(c => c.CommitteeId == committeeMemberDTO.CommitteId && c.Position == "Auditor").ToList().Count;

                        if (varAuditor < maxNoOfAuditor)
                        {
                            insertCommMember = true;
                        }
                        else
                        {
                            // result = "NoOfMembersForThisRoleExceeded";
                            insertCommMember = false;
                        }
                    }

                    else if (committeeMemberDTO.Position == "Editor")
                    {
                        var varEditor = hcDc.hcCommitteeMembers.Where(c => c.CommitteeId == committeeMemberDTO.CommitteId && c.Position == "Editor").ToList().Count;

                        if (varEditor < maxNoOfEditor)
                        {
                            insertCommMember = true;
                        }
                        else
                        {
                            // result = "NoOfMembersForThisRoleExceeded";
                            insertCommMember = false;
                        }
                    }

                    else if (committeeMemberDTO.Position == "Committee Member")
                    {
                        var varCommitteeMember = hcDc.hcCommitteeMembers.Where(c => c.CommitteeId == committeeMemberDTO.CommitteId && c.Position == "Committee Member").ToList().Count;

                        if (varCommitteeMember < maxNoOfCommMember)
                        {
                            insertCommMember = true;
                        }
                        else
                        {
                            //  result = "NoOfMembersForThisRoleExceeded";
                            insertCommMember = false;
                        }
                    }

                    if (insertCommMember)
                    {
                        string comteStart = hcDc.hcCommittees.Where(a => a.CommitteeId == committeeMemberDTO.CommitteId).FirstOrDefault().StartDate.ToString();
                        string comteEnd = hcDc.hcCommittees.Where(a => a.CommitteeId == committeeMemberDTO.CommitteId).FirstOrDefault().EndDate.ToString();

                        //Check some time it may throw exceptions in case if the comm start ,end date note exits
                        if (comteStart != null && comteEnd != null)
                        {
                            //If it's nt null then compare with the ccommiitee start & end data to make sure member''s period exceed committee period
                            DateTime comteStartDate = Convert.ToDateTime(comteStart);
                            DateTime comteEndDate = Convert.ToDateTime(comteEnd);

                            if (committeeMemberDTO.MemberFrom != null && committeeMemberDTO.MemberTo != null)
                            {
                                if (committeeMemberDTO.MemberFrom < comteStartDate || committeeMemberDTO.MemberTo > comteEndDate)
                                {
                                    result = "InvalidMemberPeriod";
                                }
                                else
                                {
                                   
                                        comiteMembr.CommitteeId = committeeMemberDTO.CommitteId;
                                        comiteMembr.MemberId = committeeMemberDTO.MemberId;
                                        comiteMembr.MemberFrom = committeeMemberDTO.MemberFrom;
                                        comiteMembr.MemberTo = committeeMemberDTO.MemberTo;
                                        comiteMembr.Position = committeeMemberDTO.Position;
                                        comiteMembr.Description = committeeMemberDTO.Description;
                                        comiteMembr.ModifiedBy = committeeMemberDTO.ModifiedBy;
                                        comiteMembr.ModifiedTime = committeeMemberDTO.ModifiedTime;
                                    
                                    hcDc.SubmitChanges();
                                    result = "Updated";
                                }


                            }
                            else
                            {
                                //this wont become nnull as we are passing date from Application logic
                            }



                        }
                        else
                        {
                            result = "InValidCommittee";
                        }
                    }



                    else
                    {
                        result = "NoOfMembersForThisRoleExceeded";

                    }
                    
                }

                hcDc.SubmitChanges();
            }
            catch
            {
                throw;
            }
            return result;
        }

        public string SaveSubCommittee(SubCommitteeDTO subCommitteeDTO)
        {
            string result = "";
            try
            {
                
                var subcomte = hcDc.hcSubCommittees.Where(n => n.SubCommitteeId == subCommitteeDTO.SubCommitteeId).SingleOrDefault();
                if (subcomte == null)
                {
                    var subComName = hcDc.hcSubCommittees.Where(a => a.SubCommitteeName == subCommitteeDTO.SubCommitteeName).SingleOrDefault();
                    if (subComName == null)
                    {

                        hcSubCommittee subCommittee = new hcSubCommittee
                        {
                            MainCommitteeId = subCommitteeDTO.MainCommitteeId,
                            SubCommitteeId = subCommitteeDTO.SubCommitteeId,
                            SubCommitteeName = subCommitteeDTO.SubCommitteeName,
                            Responibility = subCommitteeDTO.Responsibility,
                            SubCommitteePhoto = subCommitteeDTO.SubCommiteePhoto,
                            Description = subCommitteeDTO.Description,
                            ModifiedBy = subCommitteeDTO.ModifiedBy,
                            ModifiedAt = subCommitteeDTO.ModifiedTime
                        };
                        hcDc.hcSubCommittees.InsertOnSubmit(subCommittee);
                        result = "Saved";
                    }
                    else
                    {
                        //Give an error meesage to hime saying that the Sub Committee Already exists
                        result = "AlreadyExists";
                    }

                }


                else
                {
                    //Update
                    result = "Updated";
                }
                hcDc.SubmitChanges();
            }
            catch
            {
                throw;
            }
            return result;
        }

        public string SaveSubCommitteeMember(SubCommitteeMemberDTO subCommitteeMemberDTO)
        {
            string result="";
            try
            {
                var subcomteMembr = hcDc.hcSubCommitteeMembers.Where(n => n.SubCommiteeId == subCommitteeMemberDTO.SubCommitteId && n.MemberId == subCommitteeMemberDTO.MemberId).SingleOrDefault();
                if (subcomteMembr == null)
                {

                    hcSubCommitteeMember subCommitteeMember = new hcSubCommitteeMember
                    {
                        SubCommiteeId = subCommitteeMemberDTO.SubCommitteId,
                        MemberId = subCommitteeMemberDTO.MemberId,
                        Role = subCommitteeMemberDTO.Role,
                        ModifiedBy = subCommitteeMemberDTO.ModifiedBy,
                        ModifiedAt = subCommitteeMemberDTO.ModifiedTime
                    };
                    hcDc.hcSubCommitteeMembers.InsertOnSubmit(subCommitteeMember);
                    result = "Saved";
                }
                else
                {
                    result = "Updated";
                }

                hcDc.SubmitChanges();
            }
            catch
            {
                throw;
            }
            return result;
        }

        public string SaveMeeting(MeetingDTO meetingDTO)
        {
            string result = "";
            try
            {

                var meetng = hcDc.hcMeetings.Where(n => n.MeetingId == meetingDTO.MeetingId).SingleOrDefault();
                if (meetng == null)
                {
                    var meetngName = hcDc.hcMeetings.Where(a => a.Title == meetingDTO.Title).SingleOrDefault();
                    if (meetngName == null)
                    {
                        string comteStart = hcDc.hcCommittees.Where(a => a.CommitteeId == meetingDTO.CommitteeId).FirstOrDefault().StartDate.ToString();
                        string comteEnd = hcDc.hcCommittees.Where(a => a.CommitteeId == meetingDTO.CommitteeId).FirstOrDefault().EndDate.ToString();

                        DateTime comteStartDate = Convert.ToDateTime(comteStart);
                        DateTime comteEndDate = Convert.ToDateTime(comteEnd);

                        if (!(meetingDTO.HeldOn < comteStartDate) && !(meetingDTO.HeldOn > comteEndDate))
                        {
                            hcMeeting meeting = new hcMeeting
                            {
                                MeetingId = meetingDTO.MeetingId,
                                CommitteeId = meetingDTO.CommitteeId,
                                MeetingType = meetingDTO.Type,
                                Title = meetingDTO.Title,
                                Description = meetingDTO.Description,
                                ChairedBy = meetingDTO.ChairedBy,
                                HeldAt = meetingDTO.HeldAt,
                                HeldOn = meetingDTO.HeldOn,
                                ModifiedBy = meetingDTO.ModifiedBy,
                                ModifiedTime = meetingDTO.ModifiedTime
                            };
                            hcDc.hcMeetings.InsertOnSubmit(meeting);
                            result = "Saved";

                        }
                        else
                        {
                            result = "InvalidMeetingDate";
                        }

                      
                    }
                    else
                    {
                        //Error message saying meeting is already exists
                        result = "AlreadyExists";
                    }

                }
                else
                {
                    //Update
                    result = "Updated";
                }

                hcDc.SubmitChanges();
            }
            catch
            {
                throw;
            }
            return result;
        }


        public string SaveMinutes(MinutesDTO minutesDTO)
        {
            string result = "";
            try
            {
                var minute = hcDc.hcMinutes.Where(n => n.MinutesId == minutesDTO.MinutesId).SingleOrDefault();
                if (minute == null)
                {
                    var minutName = hcDc.hcMinutes.Where(a => a.MinutesTitle == minutesDTO.MinutesTitle).SingleOrDefault();
                    if (minutName == null)
                    {
                        hcMinute minutes = new hcMinute
                        {
                            MinutesId = minutesDTO.MinutesId,
                            MeetingId = minutesDTO.MeetingId,
                            MinutesTitle = minutesDTO.MinutesTitle,
                            Type = minutesDTO.Type,
                            Description = minutesDTO.Description,
                            SubmittedBy = minutesDTO.SubmittedBy,
                            ProposedBy = minutesDTO.ProposedBy,
                            SecondedBy = minutesDTO.SecondedBy,
                            FilePath = minutesDTO.FilePath,
                            ModifiedBy = minutesDTO.ModifiedBy,
                            ModifiedAt = minutesDTO.ModifiedTime
                        };
                        hcDc.hcMinutes.InsertOnSubmit(minutes);
                        result = "Saved";
                    }
                    else
                    {
                        //Give an error message saying minutes already exists
                        result = "AlreadyExists";
                    }

                }
                else
                {
                    //Update
                    result = "Updated";
                }

                hcDc.SubmitChanges();
            }
            catch
            {
                throw;
            }
            return result;
        }



        public String SaveRole(RoleDTO roleDTO)
        {
            string result = "";
            try
            {
                var rol = hcDc.hcRoles.Where(a => a.RoleId == roleDTO.roleId).SingleOrDefault();
                if (rol == null)
                {
                    hcRole role = new hcRole
                    {
                        RoleId = roleDTO.roleId,
                        Name = roleDTO.roleName,
                        Description = roleDTO.Description,
                        //CreatedBy = roleDTO.CreatedBy,
                        CreatedAt = roleDTO.ModifiedAt
                    };
                    hcDc.hcRoles.InsertOnSubmit(role);
                    hcDc.SubmitChanges();
                    result = "Saved";
                }
                else
                {
                    rol.Name = roleDTO.roleName;
                    rol.Description = roleDTO.Description;
                    //rol.ModifiedBy = roleDTO.ModifiedBy;
                    rol.ModifiedAt = roleDTO.ModifiedAt;

                    hcDc.SubmitChanges();
                    result = "Updated";
                }
                return result;
            }
            catch
            {
                throw;
            }
        }







        public List<MemberDTO> GetMemberById(Guid memberId)
        {
            return hcDc.hcMembers.Where(c => c.MemberId == memberId).Select(s => new MemberDTO
            {
                Title = s.Title,
                FirstName = s.FirstName,
                LastName = s.LastName,
                MemberId = s.MemberId,
                FullName = s.Title + ". " + s.LastName + " " + s.FirstName,
                DateOfBirth = s.DateOfBirth,
                TelePhoneNo = s.TelePhoneNo,
                Division = s.Division,
               // MobileNo = s.MobileNo,
                Email = s.Email,
                PermanantAddress = s.PermanantAddress,
                TemporaryAddress = s.TemporaryAddress,
                OrdinaryBatchYear = s.OrdinaryBatchYear,
                SchoolIndexNo = s.SchoolIndexNo,
                LifeMemberNo = s.LifeMemberNo,
                Designation = s.Designation,
                WorkingAt = s.WorkingAt,
                StudiedFrom = s.StudiedFrom,
                StudiedUpto = s.StudiedUpto,
                House = s.House,
                Description = s.Descripion,
                PhotoPath = s.PhotoPath,
                NavigationUrl = "viewMember.aspx?" + s.MemberId
            }).ToList();
        }


        public List<CommitteeDTO> GetCommitteeById(Guid committeeId)
        {
            return hcDc.hcCommittees.Where(a => a.CommitteeId == committeeId).Select(s => new CommitteeDTO
            {
                CommitteeId = s.CommitteeId,
                Title = s.Title,
                StartDate = s.StartDate,
                EndDate = s.EndDate,
                Description = s.Description,
                CommitteePhotoName = s.CommitteePhotoName,
                CommitteePhotoPath = s.CommitteePhotoPath

            }).ToList();

        }

        public List<SubCommitteeDTO> GetSubCommitteeById(Guid subCommitteeId)
        {
            return hcDc.hcSubCommittees.Where(a => a.SubCommitteeId == subCommitteeId).Select(s => new SubCommitteeDTO
            {
                MainCommitteeId = s.MainCommitteeId,
                SubCommitteeName = s.SubCommitteeName,
                Description = s.Description,
                Responsibility = s.Responibility,
                SubCommiteePhoto = s.SubCommitteePhoto,
                NavigationUrl = "",
                SubCommitteeId = s.SubCommitteeId
            }).ToList();

        }

        public List<MeetingDTO> GetMeetingById(Guid MeetingId)
        {


            return hcDc.hcMeetings.Where(a => a.MeetingId == MeetingId).Select(s => new MeetingDTO
            {
                MeetingId = s.MeetingId,
                ChairedBy = s.ChairedBy,
                CommitteeId = s.CommitteeId,
                Description = s.Description,
                HeldAt = s.HeldAt,
                HeldOn = s.HeldOn,
                ShortHeldOn = s.HeldOn.Value.ToShortDateString(),
                NavigationUrl = "",
                Title = s.Title,
                Type = s.MeetingType

            }).ToList();

        }

        public List<MinutesDTO> GetMinutesById(Guid minutesId)
        {
            List<hcMinute> allMinutes = hcDc.hcMinutes.Where(a => a.MinutesId == minutesId).ToList();
            List<MinutesDTO> minutesDTOs = new List<MinutesDTO>();


            foreach (hcMinute minute in allMinutes)
            {
                MinutesDTO minutesDTO = new MinutesDTO();

                string submittedByFirstName = hcDc.hcMembers.Where(b => b.MemberId == minute.SubmittedBy).SingleOrDefault().FirstName;
                string sumibttedByLastName = hcDc.hcMembers.Where(b => b.MemberId == minute.SubmittedBy).SingleOrDefault().LastName;

                string proposedByFirstName = hcDc.hcMembers.Where(b => b.MemberId == minute.ProposedBy).SingleOrDefault().FirstName;
                string proposedByLastName = hcDc.hcMembers.Where(b => b.MemberId == minute.ProposedBy).SingleOrDefault().LastName;

                string secondedByFirstName = hcDc.hcMembers.Where(b => b.MemberId == minute.SecondedBy).SingleOrDefault().FirstName;
                string secondedByLastName = hcDc.hcMembers.Where(b => b.MemberId == minute.SecondedBy).SingleOrDefault().LastName;

                string meetingName = hcDc.hcMeetings.Where(a => a.MeetingId == minute.MeetingId).SingleOrDefault().Title;

                minutesDTO.MinutesTitle = minute.MinutesTitle;
                minutesDTO.MeetingId = minute.MeetingId;
                minutesDTO.Description = minute.Description;
                minutesDTO.FilePath = minute.FilePath;
                minutesDTO.PropserName = proposedByLastName +"  "+ proposedByFirstName;
                minutesDTO.NavigationUrl = "viewMeetingById.aspx?" + minute.MeetingId;
                minutesDTO.SeconderName = secondedByLastName + "  " + secondedByFirstName;
                minutesDTO.MeetingName = meetingName;
                minutesDTO.SubmitterName = sumibttedByLastName + "  " + submittedByFirstName;
               // minutesDTO.Heading = minute.Heading;
                minutesDTO.Type = minute.Type;
                
                minutesDTO.SubmittedByNavigationUrl = "viewMember.aspx?" + minute.SubmittedBy;
                minutesDTO.ProposedByNavigationUrl = "viewMember.aspx?" + minute.ProposedBy;
                minutesDTO.SecondedByNavigationUrl = "viewMember.aspx?" + minute.SecondedBy;

                minutesDTOs.Add(minutesDTO);
            }
            return minutesDTOs;
        }

        public List<MeetingDTO> GetAllMeetingNameById(Guid meetingId)
        {

            List<hcMeeting> allmeetings = hcDc.hcMeetings.Where(c => c.MeetingId == meetingId).ToList();
            List<MeetingDTO> meetingNameList = new List<MeetingDTO>();
            foreach (hcMeeting meeting in allmeetings)
            {
                MeetingDTO meetingDTO = new MeetingDTO();
                var commte = hcDc.hcCommittees.Where(c => c.CommitteeId == meeting.CommitteeId).SingleOrDefault();
                meetingDTO.MeetingId = meeting.MeetingId;
                meetingDTO.ChairedBy = meeting.ChairedBy;
                meetingDTO.ChairedByName = hcDc.hcMembers.Where(c => c.MemberId == meeting.ChairedBy).SingleOrDefault().LastName +
                                           hcDc.hcMembers.Where(c => c.MemberId == meeting.ChairedBy).SingleOrDefault().FirstName; 
                meetingDTO.ChairedByNavigationUrl = "viewMember.aspx?" + meeting.ChairedBy;
                meetingDTO.HeldAt = meeting.HeldAt;
                meetingDTO.HeldOn = meeting.HeldOn;
                meetingDTO.Description = meeting.Description;
                meetingDTO.CommitteeName = commte.Title;
                meetingDTO.CommitteeNavigationUrl="viewExecutiveCommitteeById.aspx?" + commte.CommitteeId;
                meetingDTO.Title  = meeting.Title;
                meetingDTO.Type = meeting.MeetingType;
               
                //meetingDTO.MeetingName = commte.Title + "'s " + meeting.Title + " held on" + meeting.HeldOn;
                meetingNameList.Add(meetingDTO);

            }


            return meetingNameList;
        }







        public List<MemberDTO> GetAllMember()
        {
            return hcDc.hcMembers.OrderBy(c => c.FirstName).OrderBy(c => c.LastName).Select(s => new MemberDTO
            {
                MemberId = s.MemberId,
                FullName = s.Title + ". " + s.LastName + " " + s.FirstName,
                NavigationUrl = "viewMember.aspx?" + s.MemberId

            }).ToList();


        }

        public List<CommitteeDTO> GetAllCommittee()
        {
            return hcDc.hcCommittees.Select(s => new CommitteeDTO
            {
                CommitteeId = s.CommitteeId,
                Title = s.Title,
                StartDate = s.StartDate,
                EndDate = s.EndDate,
                Description = s.Description,
                CommitteePhotoName = s.CommitteePhotoName,
                CommitteePhotoPath = s.CommitteePhotoPath,
                NavigationUrl = "viewExecutiveCommitteeById.aspx?" + s.CommitteeId

            }).ToList();

        }


        public List<SubCommitteeDTO> GetAllSubCommittee()
        {
            return hcDc.hcSubCommittees.Select(s => new SubCommitteeDTO
            {
                MainCommitteeId = s.MainCommitteeId,
                SubCommitteeId = s.SubCommitteeId,
                SubCommitteeName = s.SubCommitteeName,
                Responsibility = s.Responibility,
                Description = s.Description,
                SubCommiteePhoto = s.SubCommitteePhoto,
                NavigationUrl = "viewSubCommitteeById.aspx?" + s.SubCommitteeId

            }).ToList();

        }

        public List<MeetingDTO> GetAllMeetings()
        {


            return hcDc.hcMeetings.OrderByDescending(c => c.HeldOn).Select(s => new MeetingDTO
            {
                MeetingId = s.MeetingId,
                CommitteeId = s.CommitteeId,
                ChairedBy = s.ChairedBy,
                Description = s.Description,
                HeldAt = s.HeldAt,
                HeldOn = s.HeldOn,
                NavigationUrl = "viewMeetingById.aspx?" + s.MeetingId,
                Title = s.Title,
                Type = s.MeetingType
            }).ToList();
        }


        public List<MinutesDTO> getAllMinutes()
        {
            List<hcMinute> minutes = hcDc.hcMinutes.ToList();
            Guid meetingId;
            DateTime heldOn;
            List<MinutesDTO> minutesList = new List<MinutesDTO>();
            foreach (hcMinute minute in minutes)
            {
                MinutesDTO minutesDTO = new MinutesDTO();
                meetingId = new Guid(minute.MeetingId.ToString());
                heldOn = Convert.ToDateTime(GetMeetingById(meetingId).SingleOrDefault().HeldOn.ToString());
                minutesDTO.HeldOn = heldOn;
                minutesDTO.Description = minute.Description;
                minutesDTO.FilePath = minute.FilePath;
                minutesDTO.MeetingId = minute.MeetingId;
                minutesDTO.MinutesId = minute.MinutesId;
                minutesDTO.MinutesTitle = minute.MinutesTitle;
                minutesDTO.ProposedBy = minute.ProposedBy;
                minutesDTO.SecondedBy = minute.SecondedBy;
                minutesDTO.SubmittedBy = minute.SubmittedBy;
                minutesDTO.Type = minute.Type;
                minutesDTO.NavigationUrl = "viewMinutesById.aspx?" + minute.MinutesId; ;
                minutesList.Add(minutesDTO);
            }
            return minutesList.OrderByDescending(c => c.HeldOn).ToList();
        }












        public List<SubCommitteeDTO> GetAllSubCommitteeByCommitteeId(Guid committeeId)
        {
            return hcDc.hcSubCommittees.Where(a => a.MainCommitteeId == committeeId).Select(s => new SubCommitteeDTO
            {
                MainCommitteeId = s.MainCommitteeId,
                SubCommitteeId = s.SubCommitteeId,
                SubCommitteeName = s.SubCommitteeName,
                Responsibility = s.Responibility,
                Description = s.Description,
                SubCommiteePhoto = s.SubCommitteePhoto,
                NavigationUrl = "viewSubCommitteeById.aspx?" + s.SubCommitteeId

            }).ToList();

        }



        public List<CommitteeMemberDTO> GetAllMemberOfCommitteeByCommitteeId(Guid committeeId)
        {


            List<CommitteeMemberDTO> committeeMember = new List<CommitteeMemberDTO>();

            //Add range will add as same as its added to the list
            committeeMember.AddRange(hcDc.hcCommitteeMembers.Where(a => a.CommitteeId == committeeId && a.Position == "Patron").Select(s => new CommitteeMemberDTO
            {
                CommitteId = s.CommitteeId,
                MemberId = s.MemberId,
                MemberFrom = s.MemberFrom,
                MemberTo = s.MemberTo,
                Position = s.Position,
                Description = s.Description

            }).ToList());


            committeeMember.AddRange(hcDc.hcCommitteeMembers.Where(a => a.CommitteeId == committeeId && a.Position == "Vice Patron").Select(s => new CommitteeMemberDTO
            {
                CommitteId = s.CommitteeId,
                MemberId = s.MemberId,
                MemberFrom = s.MemberFrom,
                MemberTo = s.MemberTo,
                Position = s.Position,
                Description = s.Description

            }).ToList());

            committeeMember.AddRange(hcDc.hcCommitteeMembers.Where(a => a.CommitteeId == committeeId && a.Position == "Immediate Past president").Select(s => new CommitteeMemberDTO
            {
                CommitteId = s.CommitteeId,
                MemberId = s.MemberId,
                MemberFrom = s.MemberFrom,
                MemberTo = s.MemberTo,
                Position = s.Position,
                Description = s.Description

            }).ToList());

            committeeMember.AddRange(hcDc.hcCommitteeMembers.Where(a => a.CommitteeId == committeeId && a.Position == "President").Select(s => new CommitteeMemberDTO
            {
                CommitteId = s.CommitteeId,
                MemberId = s.MemberId,
                MemberFrom = s.MemberFrom,
                MemberTo = s.MemberTo,
                Position = s.Position,
                Description = s.Description

            }).ToList());

            committeeMember.AddRange(hcDc.hcCommitteeMembers.Where(a => a.CommitteeId == committeeId && a.Position == "Vice President").Select(s => new CommitteeMemberDTO
            {
                CommitteId = s.CommitteeId,
                MemberId = s.MemberId,
                MemberFrom = s.MemberFrom,
                MemberTo = s.MemberTo,
                Position = s.Position,
                Description = s.Description

            }).ToList());

            committeeMember.AddRange(hcDc.hcCommitteeMembers.Where(a => a.CommitteeId == committeeId && a.Position == "Secretary").Select(s => new CommitteeMemberDTO
            {
                CommitteId = s.CommitteeId,
                MemberId = s.MemberId,
                MemberFrom = s.MemberFrom,
                MemberTo = s.MemberTo,
                Position = s.Position,
                Description = s.Description

            }).ToList());

            committeeMember.AddRange(hcDc.hcCommitteeMembers.Where(a => a.CommitteeId == committeeId && a.Position == "Asst.Secretary").Select(s => new CommitteeMemberDTO
            {
                CommitteId = s.CommitteeId,
                MemberId = s.MemberId,
                MemberFrom = s.MemberFrom,
                MemberTo = s.MemberTo,
                Position = s.Position,
                Description = s.Description

            }).ToList());

            committeeMember.AddRange(hcDc.hcCommitteeMembers.Where(a => a.CommitteeId == committeeId && a.Position == "Treasurer").Select(s => new CommitteeMemberDTO
            {
                CommitteId = s.CommitteeId,
                MemberId = s.MemberId,
                MemberFrom = s.MemberFrom,
                MemberTo = s.MemberTo,
                Position = s.Position,
                Description = s.Description

            }).ToList());

            committeeMember.AddRange(hcDc.hcCommitteeMembers.Where(a => a.CommitteeId == committeeId && a.Position == "Asst.Treasurer").Select(s => new CommitteeMemberDTO
            {
                CommitteId = s.CommitteeId,
                MemberId = s.MemberId,
                MemberFrom = s.MemberFrom,
                MemberTo = s.MemberTo,
                Position = s.Position,
                Description = s.Description

            }).ToList());

            committeeMember.AddRange(hcDc.hcCommitteeMembers.Where(a => a.CommitteeId == committeeId && a.Position == "Editor").Select(s => new CommitteeMemberDTO
            {
                CommitteId = s.CommitteeId,
                MemberId = s.MemberId,
                MemberFrom = s.MemberFrom,
                MemberTo = s.MemberTo,
                Position = s.Position,
                Description = s.Description

            }).ToList());

            committeeMember.AddRange(hcDc.hcCommitteeMembers.Where(a => a.CommitteeId == committeeId && a.Position == "Auditor").Select(s => new CommitteeMemberDTO
            {
                CommitteId = s.CommitteeId,
                MemberId = s.MemberId,
                MemberFrom = s.MemberFrom,
                MemberTo = s.MemberTo,
                Position = s.Position,
                Description = s.Description

            }).ToList());

            committeeMember.AddRange(hcDc.hcCommitteeMembers.Where(a => a.CommitteeId == committeeId && a.Position == "Committee Member").Select(s => new CommitteeMemberDTO
            {
                CommitteId = s.CommitteeId,
                MemberId = s.MemberId,
                MemberFrom = s.MemberFrom,
                MemberTo = s.MemberTo,
                Position = s.Position,
                Description = s.Description

            }).ToList());

           

            return committeeMember;
        }


        public List<SubCommitteeMemberDTO> GetAllSubCommitteeMemberBySubCommitteeId(Guid subCommitteeId)
        {
            List<hcSubCommitteeMember> subCommitteeMember = hcDc.hcSubCommitteeMembers.Where(b => b.SubCommiteeId == subCommitteeId).ToList();

            List<SubCommitteeMemberDTO> subCommitteeMemberList = new List<SubCommitteeMemberDTO>();
            foreach (hcSubCommitteeMember subMember in subCommitteeMember)
            {
                SubCommitteeMemberDTO subCommitteeMemberDTO = new SubCommitteeMemberDTO();
                subCommitteeMemberDTO.MemberId = subMember.MemberId;
                subCommitteeMemberDTO.Role = subMember.Role;
                subCommitteeMemberDTO.SubCommitteId = subMember.SubCommiteeId;
                subCommitteeMemberDTO.NavigationUrl = "viewMember.aspx?" + subMember.MemberId;
                subCommitteeMemberDTO.HeaderText = hcDc.hcMembers.Where(a => a.MemberId == subMember.MemberId).SingleOrDefault().FirstName + " " +
                    hcDc.hcMembers.Where(a => a.MemberId == subMember.MemberId).SingleOrDefault().FirstName;
                subCommitteeMemberList.Add(subCommitteeMemberDTO);
            }
            return subCommitteeMemberList;
        }


        public List<MeetingDTO> GetAllMeetingsOfCommitteee(Guid CommitteeId)
        {


            return hcDc.hcMeetings.Where(a => a.CommitteeId == CommitteeId).OrderByDescending(c => c.HeldOn).Select(s => new MeetingDTO
            {
                MeetingId = s.MeetingId,
                Title = s.Title,
                ChairedBy = s.ChairedBy,
                CommitteeId = s.CommitteeId,
                Description = s.Description,
                HeldAt = s.HeldAt,
                HeldOn = s.HeldOn,
                Type = s.MeetingType,
                NavigationUrl = "viewMeetingById.aspx?" + s.MeetingId

            }).ToList();

        }



        public List<MinutesDTO> GetAllMinutesByCommitteeId(Guid committeeId)
        {
            Guid meetingId;
            DateTime heldOn;
            List<MinutesDTO> minutesList = new List<MinutesDTO>();

            List<hcMeeting> allMeeting = hcDc.hcMeetings.Where(a => a.CommitteeId == committeeId).ToList();
            foreach (hcMeeting meeting in allMeeting)
            {
                List<hcMinute> allMinutes = hcDc.hcMinutes.Where(a => a.MeetingId == meeting.MeetingId).ToList();
                foreach (hcMinute minute in allMinutes)
                {
                    MinutesDTO minutesDTO = new MinutesDTO();
                    meetingId = new Guid(minute.MeetingId.ToString());
                    heldOn = Convert.ToDateTime(GetMeetingById(meetingId).SingleOrDefault().HeldOn.ToString());
                    minutesDTO.HeldOn = heldOn;
                    minutesDTO.CommitteeId = committeeId;
                    minutesDTO.Description = minute.Description;
                    minutesDTO.FilePath = minute.FilePath;
                    minutesDTO.MeetingId = minute.MeetingId;
                    minutesDTO.MinutesId = minute.MinutesId;
                    minutesDTO.MinutesTitle = minute.MinutesTitle;
                    minutesDTO.ProposedBy = minute.ProposedBy;
                    minutesDTO.SecondedBy = minute.SecondedBy;
                    minutesDTO.SubmittedBy = minute.SubmittedBy;
                    minutesDTO.Type = minute.Type;
                    minutesDTO.NavigationUrl = "viewMinutesById.aspx?" + minute.MinutesId; ;
                    minutesList.Add(minutesDTO);
                }
            }

            return minutesList;

        }

        public List<MinutesDTO> GetAllMinutesOfMeeting(Guid MeetingId)
        {


            return hcDc.hcMinutes.Where(a => a.MeetingId == MeetingId).Select(s => new MinutesDTO
            {
                MinutesId = s.MinutesId,
                MeetingId = s.MeetingId,
                Description = s.Description,
                FilePath = s.FilePath,
                MinutesTitle = s.MinutesTitle,
                NavigationUrl = "viewMinutesById.aspx?" + s.MinutesId,
                ProposedBy = s.ProposedBy,
                SecondedBy = s.SecondedBy,
                SubmittedBy = s.SubmittedBy,
                Type = s.Type

            }).ToList();

        }


        public List<CommitteeMemberDTO> getAllRolseOfCommMember(Guid comMembrId)
        {
            return hcDc.hcCommitteeMembers.Where(a => a.MemberId == comMembrId).Select(b => new CommitteeMemberDTO
            {
                CommitteId = b.CommitteeId,
                Description = b.Description,
                MemberFrom = b.MemberFrom,
                MemberTo = b.MemberTo,
                MemberId = b.MemberId,
                ModifiedBy = b.ModifiedBy,
                ModifiedTime = b.ModifiedTime,
                NavigationUrl = "viewMember.aspx?" + b.MemberId,
                Position = b.Position

            }).ToList();
        }



        public List<CommitteeMemberDTO> GetAllCommitteeMembers()
        {
            //Add range will add as same as its added to the list
            return hcDc.hcCommitteeMembers.Select(s => new CommitteeMemberDTO
            {
                CommitteId = s.CommitteeId,
                MemberId = s.MemberId,
                MemberFrom = s.MemberFrom,
                MemberTo = s.MemberTo,
                Position = s.Position,
                Description = s.Description

            }).ToList();
        }


        public List<CommitteeMemberDTO> getCommitteeMemberById(Guid comMembrId)
        {
            return hcDc.hcCommitteeMembers.Where(a => a.MemberId == comMembrId).Select(b => new CommitteeMemberDTO
            {
                CommitteId = b.CommitteeId,
                Description = b.Description,
                MemberFrom = b.MemberFrom,
                MemberTo = b.MemberTo,
                MemberId = b.MemberId,
                ModifiedBy = b.ModifiedBy,
                ModifiedTime = b.ModifiedTime,
                NavigationUrl = "viewMember.aspx?" + b.MemberId,
                Position = b.Position

            }).ToList();
        }

        public List<RoleDTO> getAllRoles()
        {
            return hcDc.hcRoles.Select(a=> new RoleDTO
                {
                    roleId = a.RoleId,
                    roleName = a.Name,
                    Description = a.Description
                }).ToList();
        }

        public String deleteRole(Guid roleId)
        {
            String result = "";
            var rol = hcDc.hcRoles.Where(a => a.RoleId == roleId).SingleOrDefault();

            if (rol != null)
            {
                try
                {
                    hcDc.hcRoles.DeleteOnSubmit(rol);
                    hcDc.SubmitChanges();
                    result = "success";
                }
                catch
                {
                    result = "error";
                }
            }
            else
            {
                result = "norecord";
            }
            return result;
        }
        public bool validateLogin(string userName, string password)
        {
            bool validUser = false;
            var user = hcDc.hcMembers.Where(m => m.UserName == userName && m.Password == password).SingleOrDefault();
            if (user != null)
            {
                validUser = false;

            }
            else
            {
                validUser = true;

            }
            return validUser;
        }

        public bool checkAvailabilityOfUserName(string userName)
        {
            bool isAvailable = false;
            var user = hcDc.hcMembers.Where(m => m.UserName == userName).SingleOrDefault();
            if (user != null)
            {
                isAvailable = false;

            }
            else
            {
                isAvailable = true;

            }
            return isAvailable;
        }




    }



}
